<?php
class Domain_WeData
{
    /**
     * 测试
     * @desc wjp 2018-1-5
     * @throws PhalApi_Exception_InternalServerError
     */
    public function test()
    {

        $appId = DI()->config->get("app.Bzy.appId");
        $openId = DI()->config->get("app.Bzy.openId");
        $masterKey = DI()->config->get("app.Bzy.masterKey");
        $timestamp = date("YmdHis");
        $data = [
            'phase'=>date("Ymd")
        ];
        $dataJson = json_encode($data);
        $baseData = base64_encode($dataJson);

        $testArray[] = $appId;
        if($baseData!=null)
            $testArray[] = $baseData;
        $testArray[] = $openId;
        $testArray[] = $timestamp;
        sort($testArray);
        $str = "";
        foreach ($testArray as $v){
            $str .=$v;
        }

        $sign = sha1($str);
//        $sign = sha1($testArray[0].$testArray[1].$openKey.$timestamp);
        $postArray = [
            "appKey"=>$appId,
            "timestamp"=> $timestamp,
            "sign"=>$sign,
            "data"=>$baseData
        ];
//        print_r($postArray);exit;
        $postJson = json_encode($postArray);
        $headers[] = 'Content-Type:application/json;charset=utf-8';
        $url = "https://sapi.8win.com/gateway/v1/data/jczq/phase";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postJson);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $result = curl_exec($ch);
        curl_close($ch);
        echo $result;exit;
    }

    public function test2(){
        $data = [
            'phase'=>date("Ymd")
        ];
        $action = "/v1/data/jczq/phase";
        $ret = DI()->bzy ->getData($data,$action);
        echo $ret;exit;
    }

    /**
     * 用户列表
     * @param $data
     * @return array|null
     */
    public function userList($data){
        $num = 20;
        $sql = "";
        if(!isset(DI() -> user['id'])){
            return null;
        }
        if( isset( $data['id']) )
            $sql .= "  && (fid = {$data['id']} || pid = {$data['id']} )";

        if( isset( $data['nickName']) )
            $sql .= "  && nickName = '{$data['nickName']}' ";


            if(DI() -> user['level'] == 4){
                if( isset( $data['fid']) )
                    $sql .= "  && fid = {$data['fid']} ";
                if( isset( $data['pid']) )
                    $sql .= "  && pid = {$data['pid']} ";
            }else{
                if( isset( $data['fid']) && $data['fid']>0)
                    $sql .= "  && fid = {$data['fid']} ";
                else{
                    $userId = DI() -> user['id'];
                    $sql .= "  && (fid = {$userId} || pid = {$userId})";
                }
            }



        if( isset( $data['level']) && $data['level'] != null)
            $sql .= "  && level = {$data['level']} ";

        $page = $data['page'];
        $page = $page - 1;
        if($page < 0)
            return null;
        $limitEnd = $num;
        $limitStart = $limitEnd*$page;
        $pageSql = $sql;
        $sql .= " ORDER by updateTime desc 
Limit {$limitStart},{$limitEnd}";
        $model = new Model_JcUser();
        $list = $model ->getUserList($sql);
        $allPage = $model ->getUserPage($pageSql,$num);
        return ['list' => $list,'allpage' => $allPage ];
    }

    public function updateUserLevel($data){
        $model = new Model_JcUser();
        $userInfo = $model -> getUserId($data['id']);

        if(DI() -> user['level'] != 4){
            if($userInfo['pid'] != DI() -> user['id']){
                return null;
            }
        }
        if(DI() -> user['level'] <= $data['level']){
            return null;
        }
        $allPage = $model ->updateUserLevel($data);
        return $allPage;
    }

    public function moneyLogList($data){
        $model = new Model_JcUser();
        $num = 20;
        $sql = "";
        if(DI() -> user['level'] != 4){
            $userId = DI() -> user['id'];
            $sql .= " walletId = 2 && userId = '$userId' ";
        }else{
            if(isset($data['id']))
                $sql .= " walletId = 2 && userId = '{$data['id']}' ";
            else{
                $userId = DI() -> user['id'];
                $sql .= " walletId = 2 && userId = '$userId' ";
            }
        }
        $page = $data['page'];
        $page = $page - 1;
        if($page < 0)
            return null;
        $limitEnd = $num;
        $limitStart = $limitEnd*$page;
        $pageSql = $sql;
        $sql .= " ORDER by updateTime desc 
Limit {$limitStart},{$limitEnd}";
        $list = $model ->getUserMoneyLog($sql);
        $allPage = $model ->getUserMoneyLogPage($pageSql,$num);
        return ['list' => $list,'allpage' => $allPage ];
    }
}